package cn.dnui_cr503.dnuifood_16110100503.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
import cn.dnui_cr503.dnuifood_16110100503.R;
import cn.dnui_cr503.dnuifood_16110100503.bean.Shop;
import cn.dnui_cr503.dnuifood_16110100503.controller.FoodListActivity;
import cn.dnui_cr503.dnuifood_16110100503.model.UserModel;

/**
 * Created by chengrui on 2018/11/11.
 */

public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<Shop> mDataList;
    private LayoutInflater mLayoutInflater;
    public ShopAdapter(Context mContext,List mDataList){
        this.mContext=mContext;
        this.mDataList=mDataList;
        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v=mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.shop_item,viewGroup,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {
        final Shop entity =mDataList.get(position);
        if(null==entity)
            return;
        ViewHolder viewHolder=(ViewHolder)holder;
        viewHolder.res_name.setText(entity.getShopname());
        viewHolder.res_address.setText(entity.getAddress());
        viewHolder.res_bar.setRating(entity.getLevel());

        Picasso.with(mContext).load(UserModel.Constants.BASE_URL+entity.getPic()).into(viewHolder.image);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到具体店铺页面
                String str = String.valueOf(mDataList.get(position).getShop_id());
                String str1 = String.valueOf(mDataList.get(position).getShopname());
                String str2 = String.valueOf(mDataList.get(position).getUser_id());

                Intent intent = new Intent(mContext,FoodListActivity.class);
                intent.putExtra("shop_id",entity.getShop_id());
                intent.putExtra("shop_id",str);
                intent.putExtra("shopname",str1);
                intent.putExtra("user_id",str2);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        RatingBar res_bar;
        TextView res_name,res_address;
        ImageView image;
        public ViewHolder(View view) {
            super(view);
            res_name=(TextView)view.findViewById(R.id.res_name);
            res_address=(TextView)view.findViewById(R.id.res_address);
            res_bar=(RatingBar) view.findViewById(R.id.ratingBar1);
            image=(ImageView)view.findViewById(R.id.res_image);
        }
    }
}
